package model.dao.registerOrder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import log.MyLogger;
import model.service.Employee;
import model.service.ServiceDevice;
import model.service.ServiceOrder;
import model.service.ServicePart;


public class ServicePartDAOImpl{
	
	String sql = "" ;
	db.dbconnection dbconnection ;
	Connection myConnection ;
	Statement st ;
	
	public List<ServicePart> getServiceParts(int serviceOrder) {
		List<ServicePart> serviceParts = new ArrayList<ServicePart>();
		
		try {
			dbconnection = new db.dbconnection();
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "SELECT * FROM service_part WHERE service_order_fk = " + serviceOrder;
			
			ResultSet r = st.executeQuery(sql);
			while (r.next()) {
				ServicePart sp = new ServicePart();
				sp.setServicePart(r.getInt("service_part"));
				sp.setServiceOrder(new ServiceOrder(r.getInt("service_order_fk")));
				sp.setServiceDevice(new ServiceDevice(r.getInt("service_device_fk"))); 
				sp.setPartName(r.getString("part_name"));
				sp.setSerialNo(r.getString("serial_no"));
				sp.setPartCount(r.getInt("part_count"));
				sp.setPartPrice(r.getFloat("part_price"));
				sp.setCreated(r.getTimestamp("created"));
				sp.setCreatedBy(new Employee(r.getInt("created_by"))); 
						
				serviceParts.add(sp);
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServicePartDAOImpl.getServiceParts()", e.getMessage());
		}
		return serviceParts;
	}

	public boolean insertServicePart(ServicePart servicePart) {
		try {
			dbconnection = new db.dbconnection();
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "INSERT INTO service_part(service_order_fk,service_device_fk,part_name,part_count,serial_no,part_price,created,created_by) " +
					"VALUES("+servicePart.getServiceOrder().getServiceOrder()+","+servicePart.getServiceDevice().getServiceDevice()+",'"+servicePart.getPartName()+"',"+servicePart.getPartCount()+",'"+servicePart.getSerialNo()+"',"+servicePart.getPartPrice()+",NOW(),"+servicePart.getCreatedBy().getEmployee()+")";
			st.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
//			MyLogger.Log("ServicePartDAOImpl.insertServicePart()", e.getMessage());
			return false;
		}
		return true;
	}

	public boolean deleteServicePart(int service_part) {
		try {
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "DELETE FROM service_part WHERE service_part="+service_part;
					
			st.executeUpdate(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServicePartDAOImpl.deleteServicePart()", e.getMessage());
			return false;
		}
		return true;
	}

	public boolean updateServicePart(ServicePart servicePart) {
		try {
			dbconnection = new db.dbconnection();
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "UPDATE service_part SET part_price="+servicePart.getPartPrice()+", part_count="+servicePart.getPartCount()+" WHERE service_part="+servicePart.getServicePart();
					
			st.executeUpdate(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServicePartDAOImpl.updateServicePart()", e.getMessage());
			return false;
		}
		return true;
	}

	public ServicePart getServicePartById(int service_part) {
		ServicePart sp = new ServicePart();
		try {
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "SELECT * FROM service_part WHERE service_part = "+service_part;
			
			ResultSet r = st.executeQuery(sql);
			while (r.next()) {
				
				sp.setServicePart(r.getInt("service_part"));
				sp.setServiceOrder(new ServiceOrder(r.getInt("service_order_fk")));
				sp.setServiceDevice(new ServiceDevice(r.getInt("service_device_fk"))); 
				sp.setPartName(r.getString("part_name"));
				sp.setSerialNo(r.getString("serial_no"));
				sp.setPartCount(r.getInt("part_count"));
				sp.setPartPrice(r.getFloat("part_price"));
				sp.setCreated(r.getTimestamp("created"));
				sp.setCreatedBy(new Employee(r.getInt("created_by"))); 

			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServicePartDAOImpl.getServiceParts()", e.getMessage());
		}
		return sp;
	}

}
